function  [eta, hat_kappa0, moments, kappap_multiplier] = adjust_eta_and_kappa0_for_avg_GDP_drop( model_sol_raw , loss_given_default )
% Usage: adjust the eta for all the solution files of Smolayak grid.

avg_GDP_drop = -0.091; 
eta_lower = -model_sol_raw.par.eta+0.001;  eta_upper = 1;  improvement = 1;  N_years=5000;  
% if( strcmp(model_sol_raw.par.model_name,'benchmark') )
%     eta_upper=20;
% end
if( isfield(model_sol_raw.par, 'iter_count_max' ) )
    iter_count_max=model_sol_raw.par.iter_count_max;
else
    iter_count_max=10; 
end

diff = 10;      iter=0;
model_sol_iter = post_processing(model_sol_raw);
while( abs(diff) > 0.01 && iter <iter_count_max && improvement>0.02 )
    par = model_sol_iter.par;   
    par.supress_credit_spread_in_simulation = true;   % Important: we do not need to solve creidt spread in this loop
    w_init=0.15;  lambda_init= (par.lambda_H + par.lambda_L)/2;  random_seed=1;
    shocks = shocks_generation( N_years,  par, random_seed);  
    sim = simulate( w_init, lambda_init,  shocks.dB_vec, shocks.dN_vec,  N_years, model_sol_iter,  par  );
    if( par.model_name == "behavioral" )    
        [ ~,  moments ] = simulation_moments( sim, model_sol_iter );
    else
        [ moments,  ~ ] = simulation_moments( sim, model_sol_iter );
    end
    avg_GDP_drop_sim = mean(moments.output_change_3_years);
    
    diff = ( avg_GDP_drop_sim - avg_GDP_drop ) / avg_GDP_drop;
    if(abs(diff) < 0.01)
        break;
    end
    if( diff>0 )
        eta_upper = model_sol_iter.par.eta;
    else
        eta_lower =  model_sol_iter.par.eta;
    end
    disp( [ 'round ', num2str(iter), ' GDP drop=', num2str(avg_GDP_drop_sim), ' with eta=',  num2str(model_sol_iter.par.eta)] );
    eta_new = max((eta_lower+eta_upper)/2,0);
    if(iter_count_max>1)
        model_sol_iter = post_processing_change_eta(model_sol_iter, eta_new);    
    end
    iter = iter+1;
end
eta = model_sol_iter.par.eta;  
shocks = shocks_generation( N_years,  par, random_seed);  
sim = simulate( w_init, lambda_init,  shocks.dB_vec, shocks.dN_vec,  N_years, model_sol_iter,  par  );
moments = simulation_moments( sim, model_sol_iter );
crises_index = moments.crises_starting_index_vec(moments.crises_starting_index_vec>10);
kappap_sim =  (  moments.p( crises_index-1 ) - moments.p(crises_index+1 )  )  ./ moments.p( crises_index-1 ) ;

kappap_multiplier = 0.1 / mean(kappap_sim);  % the multiplier over kappap should generate additional 10% of losses in GDP. This is for the credit spread pricing. 
hat_kappa0 = loss_given_default - 0.1 * par.crisis_frequency_target;  % the fixed component in credit spread pricing.  

% hat_kappa0 =  max(  loss_given_default -  mean(kappap_sim),  0  );
avg_GDP_drop_sim = mean(moments.output_change_3_years);
% disp( [ 'hat_kappa0 adjusted to ', num2str(hat_kappa0)] )


